package cn.e4j.modular.api.controller;

import cn.digiwin.wx.utils.WeixinHandler;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.IDto;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.service.FwpgService;
import cn.e4j.modular.api.service.Gwat200Service;
import cn.e4j.modular.system.entity.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yuan_rz on 2018/04/12.
 */
@RestController
@RequestMapping("api/wx/fwpg")
public class FwpgController {
    private static Logger logger = LoggerFactory.getLogger(FwpgController.class);

    @Autowired
    private FwpgService fwpgService;

    @Autowired
    Gwat200Service gwat200Service;

    /**
     * 获取服务单列表
     *
     * @return
     */
    @RequestMapping(value = "getServiceOrderListData")
    public ResponseData getServiceOrderListData(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<Dto>();
        SysUser currentUser = wapper.getSysUser();
        try {
//            / 获取区域权限-进行省市过滤数据
            inDto.put("GWAA003", currentUser.getACCOUNT());
            inDto.put("MMRE004S", gwat200Service.getMmreData(inDto));

//            inDto.put("mmre001", currentUser.getOrg_id_());//组织id
            inDto.put("MMRDDOCDATE", DateUtils.parseDate(inDto.getString("MMRDDOCDATE")));
            list = fwpgService.getServiceOrderListData(wapper.getPage(), inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务器出小差了");
        }
        return new SuccessResponseData(list);
    }

    /**
     * 获取维修人员
     *
     * @return
     */
    @RequestMapping(value = "getUserListData")
    public ResponseData getUserListData(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<Dto>();
        SysUser currentUser = wapper.getSysUser();
        try {
            inDto.put("GWAKENT", inDto.getString("ENT"));
            //inDto.put("gwak001","1");
            inDto.put("GWAK001", currentUser.getID());
            inDto.put("MMREENT", inDto.getString("ENT"));

            list = fwpgService.getUserListData(wapper.getPage(), inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务器出小差了");
        }
        return new SuccessResponseData(list);
    }

    /**
     * 服务派工提交
     *
     * @return
     */
    @RequestMapping(value = "saveFwpg")
    public ResponseData saveFwpg(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<Dto>();
        try {
            list = fwpgService.saveFwpg(wapper.getSysUser(), inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务器出小差了");
        }

        try {
            if (ToolUtil.isNotEmpty(list)){
                list.forEach(dto->{
                    //品名
                    String gwaml003 = dto.getString("GWAML003");
                    String mmrf008 = dto.getString("mmrf008");
                    String gwakl004 = dto.getString("gwakl004");
                    if (ToolUtil.isEmpty(gwaml003))
                        logger.error("消息中品名获取为空！");
                    else {
                        String userId = dto.getString("pguser");
                        //派工人员
                        Dto pgDto = Dtos.newDto();
                        pgDto.put("userid", userId);
                        pgDto.put("GWAAENT", inDto.getString("ENT"));
                        Dto gwaa2 = fwpgService.getGwaa2(pgDto);

                        if (ToolUtil.isEmpty(gwaa2))
                            logger.error("消息中派工人员获取为空！");
                        else {
                            //派工消息
                            String pgmsg = String.format("设备[%s]已安排您去维修，请注意！",gwaml003);
                            WeixinHandler.sendMsg(gwaa2,pgmsg);

                            String gwaa002 = gwaa2.getString("NAME");
                            String gwaa004 = gwaa2.getString("GWAA004");
                            //预约者
                            String yyuser = dto.getString("yyuser");

                            if (ToolUtil.isEmpty(yyuser))
                                logger.error("消息中预约者获取为空！");
                            else {
                                Dto yyDto = new Dto();
                                yyDto.put("GWAA001",yyuser);
                                yyDto.put("GWAAENT", inDto.getString("ENT"));
                                Dto gwaa21 = fwpgService.getGwaa2(yyDto);

                                if (ToolUtil.isNotEmpty(gwaa21) && gwaa002!=null){
                                    //预约者消息
                                    String msg = String.format("[%s]客户的[%s]设备已经安排[%s]工程师电话[%s]去服务，预计上门时间为[%s]！",gwakl004,gwaml003,gwaa002,gwaa004,mmrf008);
                                    WeixinHandler.sendMsg(gwaa21,msg);
                                }
                            }

                        }
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new SuccessResponseData(list);
    }

}
